﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace MusaDor.Admin
{
    public partial class Users : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void DeleteUser_Click(object sender, EventArgs e)
        {
            Label User = (Label)UsersDetails.FindControl("NameOfUser");

            try
            {
                String DU = User.Text;
                Membership.DeleteUser(DU);
            }
            catch (Exception exp)
            {

            }
        }

        protected void UserList_ItemCommand(object sender, ListViewCommandEventArgs e)
        {

        }

        protected void Admin_Click(object sender, EventArgs e)
        {
            Label UserLabel = (Label)UsersDetails.FindControl("NameOfUser");
            String NOU = UserLabel.Text;
            bool UserInPublisher;
            UserInPublisher = Roles.IsUserInRole(NOU, "Publisher");
            bool UserInJustUser;
            UserInJustUser = Roles.IsUserInRole(NOU, "JustUser");
            bool UserInAdministrator;
            UserInAdministrator = Roles.IsUserInRole(NOU, "Administrator");

            if (UserInPublisher == true)
            {
                Roles.RemoveUserFromRole(NOU, "Publisher");
            }
            
            
            if (UserInJustUser == true)
            {
                Roles.RemoveUserFromRole(NOU, "JustUser");
            }

            if (UserInAdministrator == true)
            {
            }
            else
            {
                Roles.AddUserToRole(NOU, "Administrator");
                RoleOfUser.Text = "Administrator";
            }
        }

        protected void Publisher_Click(object sender, EventArgs e)
        {
            Label UserLabel = (Label)UsersDetails.FindControl("NameOfUser");
            String NOU = UserLabel.Text;
            bool UserInPublisher;
            UserInPublisher = Roles.IsUserInRole(NOU, "Publisher");
            bool UserInJustUser;
            UserInJustUser = Roles.IsUserInRole(NOU, "JustUser");
            bool UserInAdministrator;
            UserInAdministrator = Roles.IsUserInRole(NOU, "Administrator");

            if (UserInAdministrator == true)
            {
                Roles.RemoveUserFromRole(NOU, "Administrator");
            }

            if (UserInJustUser == true)
            {
                Roles.RemoveUserFromRole(NOU, "JustUser");
            }

            if (UserInPublisher == true)
            {
            }
            else
            {
                Roles.AddUserToRole(NOU, "Publisher");
                RoleOfUser.Text = "Publisher";
            }
        }

        protected void JustUser_Click(object sender, EventArgs e)
        {
            Label UserLabel = (Label)UsersDetails.FindControl("NameOfUser");
            String NOU = UserLabel.Text;
            bool UserInPublisher;
            UserInPublisher = Roles.IsUserInRole(NOU, "Publisher");
            bool UserInJustUser;
            UserInJustUser = Roles.IsUserInRole(NOU, "JustUser");
            bool UserInAdministrator;
            UserInAdministrator = Roles.IsUserInRole(NOU, "Administrator");

            if (UserInAdministrator == true)
            {
                Roles.RemoveUserFromRole(NOU, "Administrator");
            }

            if (UserInPublisher == true)
            {
                Roles.RemoveUserFromRole(NOU, "Publisher");
            }

            if (UserInJustUser == true)
            {
            }
            else
            {
                Roles.AddUserToRole(NOU, "JustUser");
                RoleOfUser.Text = "User";
            }
        }

        protected void UserList_SelectedIndexChanged(object sender, EventArgs e)
        {
            Role.Visible = true;
            DelUser.Visible = true;
            RoleOfUser.Visible = true;
            AddToRole.Visible = true;
            Section.Visible = true;
            RoleOfUser.Visible = true;

            Object NameOfUser = UserList.SelectedDataKey.Value;
            String NOU = Convert.ToString(NameOfUser);

            if (Roles.IsUserInRole(NOU, "Administrator"))
            {
                RoleOfUser.Text = "Administrator";
            }
            else if (Roles.IsUserInRole(NOU, "Publisher"))
            {
                RoleOfUser.Text = "Publisher";
            }
            else if (Roles.IsUserInRole(NOU, "Publisher"))
            {
                RoleOfUser.Text = "Publisher";
            }
            else
            {
                RoleOfUser.Text = "The role is not assigned";
            }
        }

        protected void DelUser_Click(object sender, EventArgs e)
        {

        }
    }
}